const gulp = require('gulp');
const del = require('del');
const runSequence = require('run-sequence');
const $ = require('gulp-load-plugins')();

let prod = false;

// -------------------- Lint ---------------------------
function swallowError(error) {
  console.error(error.toString())
  this.emit('end')
}

// -------------------- JSON 文件 ----------------------

gulp.task('json', () => {
  return gulp.src('./src/**/*.json')
    .pipe($.if(prod, $.jsonminify())).on('error', swallowError)
    .pipe(gulp.dest('./dist'))
});

gulp.task('json:watch', () => {
  gulp.watch('./src/**/*.json', ['json'])
});

// -------------------- Image Assets ---------------------

gulp.task('assets:images', () => {
  return gulp.src('./src/assets/images/**')
  // .pipe($.if(prod, $.imagemin()))
    .pipe(gulp.dest('./dist/assets/images'))
});

gulp.task('assets:images:watch', () => {
  gulp.watch('./src/assets/images/**', ['assets:images'])
});

// -------------------- Other Assets ---------------------

gulp.task('assets:extras', () => {
  return gulp.src([
    './src/assets/**',
    '!./src/assets/images/**'
  ]).pipe(gulp.dest('./dist/assets'))
});

gulp.task('assets:extras:watch', () => {
  gulp.watch([
    './src/assets/**',
    '!./src/assets/images/**'
  ], ['assets:extras'])
});

gulp.task('libs', () => {
  return gulp.src('./src/libs/**/*.js').pipe(gulp.dest('./dist/libs'))
});

gulp.task('libs:watch', () => {
  gulp.watch('./src/libs/**/*.js', ['libs'])
});

// -------------------- Image 图片文件 ---------------------

gulp.task('templates', () => {
  return gulp.src('./src/**/*.wxml')
    .pipe($.if(prod, $.htmlmin({collapseWhitespace: true}))).on('error', swallowError)
    .pipe(gulp.dest('./dist'))
});

gulp.task('templates:watch', () => {
  gulp.watch('./src/**/*.wxml', ['templates'])
});

// -------------------- SCSS 文件处理 ----------------------

gulp.task('styles', () => {
  return gulp.src('./src/**/*.wxss')
    .pipe($.sass({
      outputStyle : prod?null:'expanded'
    }).on('error', $.sass.logError))
    .pipe($.if(prod, $.cssnano()))
    .pipe($.extReplace('.wxss'))
    .pipe(gulp.dest('./dist'))
});

gulp.task('styles:watch', () => {
  gulp.watch('./src/**/*.wxss', ['styles'])
});

// -------------------- JS  ------------------------

gulp.task('scripts', () => {
  return gulp.src('./src/**/*.js')
    .pipe($.if(!prod, $.sourcemaps.init())).on('error', swallowError)
    .pipe($.babel()).on('error', swallowError)
    .pipe($.if(prod, $.uglify()))
    .pipe($.if(!prod, $.sourcemaps.write('.')))
    .pipe(gulp.dest('./dist'))
});

gulp.task('scripts:watch', () => {
  gulp.watch('./src/**/*.js', ['scripts'])
});

// ------------------------------------------------------

gulp.task('clean', () => {
  return del(['./dist/**'])
});

gulp.task('build', [
  'json',
  'assets:images',
  'assets:extras',
  'libs',
  'templates',
  'styles',
  'scripts'
]);

gulp.task('watch', [
  'json:watch',
  'assets:images:watch',
  'assets:extras:watch',
  'libs:watch',
  'templates:watch',
  'styles:watch',
  'scripts:watch'
]);


gulp.task('build:clean', (callback) => {
  runSequence('clean', 'build', callback)
});

gulp.task('watch:clean', (callback) => {
  runSequence('build:clean', 'watch', callback)
});

gulp.task('build:prod', (callback) => {
  prod = true;
  runSequence('build:clean', callback)
});

gulp.task('default', ['watch:clean']);
